/*
 * PersonalNotesGenerator.java
 *
 *  created: 30.8.2011
 *  charset: UTF-8
 *  license: MIT (X11) (See LICENSE file for full license)
 */
package cz.mp.k3bg.core;

import static cz.mp.k3bg.Application.EOL;
import cz.mp.k3bg.log.LoggerManager;
import cz.mp.k3bg.util.TagUtils;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

/**
 * Třída {@code PersonalNotesGenerator} vytváří jednoduchý XHTML dokument s
 * osobními poznámkami autora jako je věnování, citát, báseň.
 * Tyto poznámky je zvykem psát na začátku knihy, před vlastním textem knihy.
 * 
 * @author Martin Pokorný
 * @version 0.1
 */
public class PersonalNotesGenerator extends Generator {
    
    private static final boolean DEBUG = false;
    private static final Logger logger =
            LoggerManager.getLogger(PersonalNotesGenerator.class, DEBUG);    
    
    private ArrayList<Note> notes = new ArrayList<Note>();

    // -----

    /**
     * 
     * @param bookFiles
     */
    public PersonalNotesGenerator(BookFiles bookFiles) {
        super(bookFiles, null);
    }

     
    
    /**
     * 
     * @throws IOException 
     */
    @Override
    public void generate() throws IOException {
        logger.info("");
        if (bookFiles.getPersonalNotes() == null) {
            throw new IllegalStateException("personalNotes=null");
        }        
        
        BufferedWriter writer = 
                bookFiles.getPersonalNotes().getBufferedWriter();
        
        HtmlHeaderBuilder headerBuilder = new HtmlHeaderBuilder();
        headerBuilder.setCharsetName(
                bookFiles.getPersonalNotes().getCharsetName());
                
        // sestavení souboru :
        writer.write(headerBuilder.createHtmlHeader());
        writer.flush();
        
        for (Note note : notes) {
            writer.write(createNewPageTag());
            
            if (note instanceof Citation) {
                writer.write(createCitation((Citation)note));
            }
            else if (note instanceof Dedication) {
                writer.write(createDedication((Dedication)note));
            }
            else {
                writer.write("<p>" + note.getText() + "</p>");
            }
        }
        
        writer.write(headerBuilder.createHtmlFooter());
        writer.flush();
        
        writer.flush();
        writer.close();
        
        bookFiles.getPersonalNotes().initExisting();
                
        logger.fine("(finished!)");
    }

    /**
     * 
     * @return 
     */
    private String createNewPageTag() {
        logger.finer("");
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"")
                .append(StyleClassNames.PAGEBREAK_BEFORE.getRealName())
                .append("\" />"+EOL);        
        return sb.toString();
    }
    
    /**
     * 
     * @param citation
     * @return
     * @throws IOException 
     */
    private String createCitation(Citation citation) throws IOException {
        logger.finer("");
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"")
                .append(StyleClassNames.MAIN_CITATION.getRealName())
                .append("\">")
                .append(EOL);
        sb.append(
                TagUtils.convertToPrimitiveHtml(citation.getText()));
        sb.append("    <div class=\"")
                .append(StyleClassNames.MAIN_CITATION_AUTHOR.getRealName())
                .append("\">")
                .append(EOL);
        sb.append("    ").append(
                TagUtils.convertToPrimitiveHtml(citation.getAuthor()));
        sb.append("    </div>").append(EOL);
        sb.append("</div>").append(EOL).append(EOL);
        return sb.toString();
    }
    
    /**
     * 
     * @param dedication
     * @return
     * @throws IOException 
     */
    private String createDedication(Dedication dedication) throws IOException {
        logger.finer("");
        StringBuilder sb = new StringBuilder();
        sb.append("<div class=\"")
                .append(StyleClassNames.BOOK_DEDICATION.getRealName())
                .append("\">")
                .append(EOL);
        sb.append(
                TagUtils.convertToPrimitiveHtml(dedication.getText()));
        sb.append("</div>").append(EOL).append(EOL);
        return sb.toString();
    }    
    
    /**
     * Na konec přidá další citát.
     * 
     * @param text
     * @param author 
     */
    public void addCitation(String text, String author) {
        notes.add(new Citation(text, author));
    }
        
    /**
     * Na konec přidá další věnování.
     * 
     * @param text 
     */
    public void addDedication(String text) {
        notes.add(new Dedication(text));
    }

    
   
    
    /**
     * 
     */
    private class Note {
        String text;

        public Note(String text) {
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }   // Note
    
    /**
     * 
     */
    private class Dedication extends Note {
        public Dedication(String text) {
            super(text);
        }        
    }   // Dedication
    
//    /**
//     * 
//     */
//    private class Thanks extends Note {
//        public Thanks(String text) {
//            super(text);
//        }        
//    }   // Thanks
    
    /**
     * 
     */
    private class Citation extends Note {
        String author;

        public Citation(String text) {
            super(text);
        }

        public Citation(String text, String author) {
            super(text);
            this.author = author;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }
    }   // Citation
    
//    /**
//     * 
//     */
//    private class Poem extends Citation {
//        public Poem(String text) {
//            super(text);
//        }
//
//        public Poem(String text, String author) {
//            super(text, author);            
//        }
//    }   // Poem
    
}   // PersonalNotesGenerator.java

